#!/usr/bin/env python3
# encoding: utf-8
"""
@author: kevin
@contact: evil365@gmail.com
@project : flask
@file: client.py
@time: 2020/8/4 10:12
@license: (C) Copyright 2019-2025, Corporation Limited.
@desc:
"""

import math, time
from app.models.zf_model import client, clientbank
from .errorCode import ClientNotFound, ClientAllExist, CardNotFound, CardAllExist

now = time.strftime('%Y-%m-%d %H:%M:%S')


class my_client(client):

    @classmethod
    def get_user_count(cls):
        count = cls.query.filter_by(delete_time=None).count()
        if not count:
            raise ClientNotFound()
        return count

    @classmethod
    def get_user_by_name(cls, key):
        _client = cls.query.filter(cls.userName.like('%' + key + '%')).filter_by(delete_time=None).all()
        if not _client:
            raise ClientNotFound()
        return _client

    @classmethod
    def get_user_by_card(cls, card_no):
        _client = cls.query.outerjoin(clientbank, cls.sn == clientbank.userSn).filter_by(card_no=cls.cardNo).filter_by(delete_time=None).all()
        if not _client:
            raise ClientNotFound()
        return _client

    @classmethod
    def get_user(cls, user_sn):
        _client = cls.query.filter_by(sn=user_sn).filter_by(delete_time=None).first()
        if not _client:
            raise ClientNotFound
        return _client

    @classmethod
    def get_all_users(cls, page):
        num = 10
        page = int(page)
        if page == 1:
            skip = 0
        else:
            skip = (page - 1) * num
        client_list = cls.query.filter_by(delete_time=None).order_by(cls.sn.desc()).limit(num).offset(skip).all()
        count = cls.get_user_count()
        pages = math.ceil(count / num)
        if client_list:
            client_data = []
            for i in client_list:
                client_data.append(i)
            client_list = {'list': client_data, 'total': count, 'page': pages}
        else:
            raise ClientNotFound
        return client_list

    @classmethod
    def new_user(cls, form):
        _client = cls.query.filter_by(cardNo=form.cardNo, delete_time=None).first()
        if _client is not None:
            raise ClientAllExist
        if form.registTime.data is None:
            form.registTime.data = now
        cls.create(
            srcId=form.srcSn.data,
            userName=form.userName.data,
            cardNo=form.cardNo.data,
            phone=form.phone.data,
            type=form.type.data,
            registTime=form.registTime.data,
            commit=True
        )
        return True

    @classmethod
    def update_user(cls, usn, form):
        _client = cls.query.filter_by(sn=usn, delete_time=None).first()
        if _client is None:
            raise ClientNotFound
        if form.registTime.data is None:
            form.registTime.data = now
        form.registTime.data = form.registTime.data.replace("T", " ").replace("Z", "")
        _client.update(
            sn=usn,
            srcId=form.srcSn.data,
            userName=form.userName.data,
            cardNo=form.cardNo.data,
            phone=form.phone.data,
            type=form.type.data,
            registTime=form.registTime.data,
            commit=True
        )
        return True

    @classmethod
    def cancell_user(cls, usn):
        _client = cls.query.filter_by(sn=usn, delete_time=None).first()
        if not _client:
            raise ClientNotFound
        _client.delete(commit=True)
        bank = clientbank.query.filter_by(userId=usn, delete_time=None).first()
        while bank:
            bank.delete(commit=True)
        return cls.get_all_client()

    @classmethod
    def new_user_bank(cls, client_sn, form):
        _bank = clientbank.query.filter_by(cardNo=form.cardNo).filter_by(delete_time=None).first()
        if _bank is not None:
            raise CardAllExist
        clientbank.create(
            userId=form.userSn.data,
            cardNo=form.cardNo.data,
            status=form.status.data,
            assetType=form.assetType.data,
            pId=form.pSn.data,
            commit=True
        )

    @classmethod
    def update_user_bank(cls, client_sn, form):
        _bank = clientbank.query.outerjoin(cls, cls.sn == clientbank.userSn).filter_by(delete_time=None).first()
        if _bank is not None:
            raise CardAllExist
        clientbank.update(
            sn=form.sn.data,
            userId=client_sn,
            cardNo=form.cardNo.data,
            status=form.status.data,
            assetType=form.assetType.data,
            pId=form.pSn.data,
            commit=True
        )

    @classmethod
    def get_bank_card(cls, client_sn):
        bank_card = clientbank.query.filter(clientbank.userSn == client_sn).filter_by(delete_time=None).all()
        if not bank_card:
            raise CardNotFound
        return bank_card
